// ignore_for_file: dangling_library_doc_comments, slash_for_doc_comments

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:webview_flutter/webview_flutter.dart';

/*** 
 * @Author: Zhong
 * @Date: 2025-08-18 15:30:41
 * @LastEditors: Zhong zhongxiaodong@eenet.com
 * @LastEditTime: 2025-08-20 17:34:58
 * @FilePath: /wan_android_flutter/lib/common_code/pages/common_webview.dart
 * @Description: 
 */

class CommonWebview extends StatefulWidget {
  const CommonWebview({super.key});

  static const String routeName = '/common_webview';

  @override
  State<CommonWebview> createState() => _CommonWebviewState();
}

class _CommonWebviewState extends State<CommonWebview> {
  late final String url;
  late final WebViewController _controller;

  final RefreshController _refreshController = RefreshController(
    initialRefresh: false,
  );

  @override
  void initState() {
    super.initState();
    url = Get.arguments["url"];

    _controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // 加载进度更新
            debugPrint('WebView加载进度: $progress%');
          },
          onPageStarted: (String url) {
            debugPrint('开始加载: $url');
            EasyLoading.show(status: "加载中...");
          },
          onPageFinished: (String url) {
            debugPrint('加载完成: $url');
            EasyLoading.dismiss();
          },
          onWebResourceError: (WebResourceError error) {
            debugPrint('加载错误: ${error.description}');
            EasyLoading.dismiss();
          },
        ),
      )
      ..loadRequest(Uri.parse(url)); // 这里替换为你的URL
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('详情'),
        // This drop down menu demonstrates that Flutter widgets can be shown over the web view.
      ),
      body: SmartRefresher(
        controller: _refreshController,
        enablePullDown: true,
        enablePullUp: true,
        onRefresh: _onRefresh,
        onLoading: _onLoading,
        header: ClassicHeader(),
        footer: CustomFooter(
          builder: (BuildContext context, LoadStatus? mode) {
            Widget body;
            if (mode == LoadStatus.idle) {
              body = Text("继续上拉加载更多！");
            } else if (mode == LoadStatus.loading) {
              body = CupertinoActivityIndicator();
            } else if (mode == LoadStatus.failed) {
              body = Text("加载失败，点击重试!");
            } else if (mode == LoadStatus.canLoading) {
              body = Text("释放加载更多！");
            } else {
              body = Text("没有更多数据了！");
            }
            return Container(height: 55.0, child: Center(child: body));
          },
        ),
        child: WebViewWidget(controller: _controller),
      ),
    );
  }

  void _onRefresh() async {
    debugPrint("下拉刷新");
    // monitor network fetch
    await Future.delayed(Duration(milliseconds: 1000));
    // if failed,use refreshFailed()
    _refreshController.refreshCompleted();
  }

  void _onLoading() async {
    debugPrint("上拉加载更多");
    // monitor network fetch
    await Future.delayed(Duration(milliseconds: 1000));
    // if failed,use loadFailed(),if no data return,use LoadNodata()
    // items.add((items.length + 1).toString());
    if (mounted) setState(() {});
    _refreshController.loadComplete();
  }
}
